package DEEPERsource.DEEPERsource.source.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PPIMapping {
	
	Map<String, String> map = new HashMap<String, String>(); 
	Map<String, List<String>> invMap = new HashMap<String, List<String>>();
	
	public PPIMapping(String dataset){
		try {
			String regex = "", fileName = "";
			if(dataset.equals("AIMed")){
				fileName = "resource/aimed.xml";
				regex = "<document id=\"(AIMed.d\\d+)\" filename=\"([^\"]+)\">";
			}else if(dataset.equals("LLL")){
				fileName = "resource/lll.xml";
				regex = "<document id=\"(LLL.d\\d+)\" origId=\"([^\"]+)\">";
			}
			BufferedReader reader = new BufferedReader(new FileReader(fileName));
			String s;
			Pattern p = Pattern.compile(regex);
			while((s = reader.readLine())!=null){
				Matcher m = p.matcher(s);
				if(m.find()){
					String key = m.group(1) + ".";
					String fn = m.group(2);
					map.put(key, fn);
					List<String> list = invMap.get(fn);
					if(list == null){
						list = new ArrayList<String>();
						invMap.put(fn, list);
					}		
					list.add(key);
				}
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String map(String id){
		return map.get(id);
	}
	
	public List<String> invMap(String fn){
		return invMap.get(fn);
	}
}
